package me.zhengjie.threePlc;

import lombok.RequiredArgsConstructor;
import me.zhengjie.agvSend.AgvSendService;
import me.zhengjie.config.IdWorker;
import me.zhengjie.config.LockConfig;
import me.zhengjie.pojo.dto.*;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.sqlservice.*;
import me.zhengjie.state.MaterialStatus;
import me.zhengjie.state.PriorityEnum;
import me.zhengjie.state.TaskStatus;
import me.zhengjie.state.TaskType;
import me.zhengjie.tcp.OutboundLineBody;
import net.bytebuddy.implementation.bytecode.Throw;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class LineBodyOutboundTiming {
    private static final Logger lineBodyLog = LoggerFactory.getLogger("lineBody");

    private final WcsLineBodyWaitOutboundService wcsLineBodyWaitOutboundService;

    private final WcsLineBodyOutboundService wcsLineBodyOutboundService;

    private final OutboundLineBody outboundLineBody;

    private final AgvSendService agvSendService;

    private final WcsThreeFloorOutboundDetailsService wcsThreeFloorOutboundDetailsService;

    private final WcsLineBodyPalletService wcsLineBodyPalletService;

    private final WcsLineBodyPalletHistoryService wcsLineBodyPalletHistoryService;

    private final WcsThreeFloorOutboundService wcsThreeFloorOutboundService;

    private final WcsConfigurationService wcsConfigurationService;

    private final IwsStoreOutsideService iwsSOutsideService;

    private final IdWorker idWorker;

    private final WcsDeliveryMissionService wcsDeliveryMissionService;

    private final WcsLineEdgeWarehouseService wcsLineEdgeWarehouseService;

    @Scheduled(fixedDelay = 1000)
//    @Transactional
    public void lineBody() {

        try {
            WcsLineBodyOutbound wcsLineBodyOutbound = wcsLineBodyOutboundService.selectOne();
            if (wcsLineBodyOutbound != null) {
                Integer lineBodyOutboundMove = outboundLineBody.readHoldingRegisters(1, 1);
                if (lineBodyOutboundMove != null && lineBodyOutboundMove == 2) {
                    Integer oneRequestPut = wcsLineBodyOutbound.getOneRequestPut();
                    Integer unitId = wcsLineBodyOutbound.getUnitId();
                    String ctuPutStorage = wcsLineBodyOutbound.getCtuPutStorage();
                    //第一段返回结果（0：无任务；1：任务中；2：已完成）
                    Integer oneTeturnResult = wcsLineBodyOutbound.getOneTeturnResult();
                    Integer wcsLineBodyOutboundListId = wcsLineBodyOutbound.getId();
                    Integer oneWritePlc = wcsLineBodyOutbound.getOneWritePlc();
                    Integer oneAddress = wcsLineBodyOutbound.getOneAddress();
                    Integer roboticArmIsThereStock = wcsLineBodyOutbound.getRoboticArmIsThereStock();
                    Integer roboticArmReadCode = wcsLineBodyOutbound.getRoboticArmReadCode();
                    Integer roboticArmRequestStacking = wcsLineBodyOutbound.getRoboticArmRequestStacking();
                    //            Integer roboticArmRequestStackingTwo = wcsLineBodyOutbound.getRoboticArmRequestStackingTwo();

                    //第一段CTU放货
                    //            List<WcsLineBodyWaitOutbound> wcsLineBodyWaitOutbounds = wcsLineBodyWaitOutboundService.selectList();
                    //            if (!wcsLineBodyWaitOutbounds.isEmpty()) {
                    //                //读取plc地址位，是否能放货
                    //                Integer oneRequestPutStatus = outboundLineBody.readHoldingRegisters(oneRequestPut, unitId);
                    //                System.out.println("读取plc地址位，是否能放货"+oneRequestPutStatus);
                    //                if (oneRequestPutStatus == 1) {
                    //                    //标记为0，agv才能放货
                    //                    if (oneWritePlc == 0) {
                    //                        //plc请求放货，发送agv进行执行当前任务
                    //                        WcsLineBodyWaitOutbound wcsLineBodyWaitOutbound = wcsLineBodyWaitOutbounds.get(0);
                    //                        Integer id = wcsLineBodyWaitOutbound.getId();
                    //                        AGVTaskContinueExecuteDto agvTaskContinueExecuteDto = new AGVTaskContinueExecuteDto();
                    //                        agvTaskContinueExecuteDto.setTriggerType("TASK");
                    //                        agvTaskContinueExecuteDto.setTriggerCode(wcsLineBodyWaitOutbound.getTaskCode());
                    //                        ContinueExecute continueExecute = new ContinueExecute();
                    //                        continueExecute.setSeq(0);
                    //                        continueExecute.setType(TaskType.SITE.getMessage());
                    //                        continueExecute.setCode(ctuPutStorage);
                    //                        agvTaskContinueExecuteDto.setTargetRoute(continueExecute);
                    //                        boolean b = agvSendService.taskContinueExecute(agvTaskContinueExecuteDto);
                    //                        if (!b) {
                    //                            lineBodyLog.error("CTU继续执行任务下发数据");
                    //                            throw new RuntimeException("CTU继续执行任务下发数据");
                    //                        }
                    //                        //通知PLC放箱任务中
                    //                        boolean b1 = outboundLineBody.writeWithRetry(oneTeturnResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                    //                        //修改标记为1
                    //                        boolean b3 = wcsLineBodyOutboundService.updateOneWritePlc(wcsLineBodyOutboundListId, 1);
                    //                        //删除
                    //                        boolean b2 = wcsLineBodyWaitOutboundService.removeId(id);
                    //                        if (!b2) {
                    //                            lineBodyLog.error("删除继续执行任务" + wcsLineBodyWaitOutbound.getTaskCode() + "失败");
                    //                            throw new RuntimeException("删除继续执行任务" + wcsLineBodyWaitOutbound.getTaskCode() + "失败");
                    //                        }
                    //                    }
                    //                }
                    //            }

                    //            Integer oneAddressStatus = outboundLineBody.readHoldingRegisters(oneAddress, unitId);
                    //            lineBodyLog.info("第一段是否有货:{},{},数据库状态：{}", oneAddressStatus, oneAddress, oneWritePlc);
                    //            if (oneWritePlc == 1 && oneAddressStatus == 0) {
                    //                //不请求将标记改为0
                    //                boolean b3 = wcsLineBodyOutboundService.updateOneWritePlc(wcsLineBodyOutboundListId, 0);
                    //            }

                    //扫码器储位
                    Integer readCodeWritePlc = wcsLineBodyOutbound.getReadCodeWritePlc();
                    Integer sweepCodeStatus = wcsLineBodyOutbound.getSweepCodeStatus();
                    Integer sweepStatus = outboundLineBody.readHoldingRegisters(sweepCodeStatus, unitId);
                    if (sweepStatus == 1) {
                        //读码状态完成
                        Integer readCodeStatus = wcsLineBodyOutbound.getReadCodeStatus();
                        Integer read = outboundLineBody.readHoldingRegisters(readCodeStatus, unitId);
                        if (read == 1) {
                            //读码完成，拿到读码信息
                            Integer readCodeInformation = wcsLineBodyOutbound.getReadCodeInformation();
                            String container = outboundLineBody.readHoldingRegistersString(readCodeInformation, unitId);
                            lineBodyLog.info("三楼线体扫码结果：{}", container);
                            //根据容器编码拿到工单号
                            Integer readCodeResult = wcsLineBodyOutbound.getReadCodeResult();
                            Integer readHoldingRegisters = outboundLineBody.readHoldingRegisters(readCodeResult, unitId);
                            WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = wcsThreeFloorOutboundDetailsService.selectContainer(container);
                            lineBodyLog.info("更新料箱找到出库数据：{}", wcsThreeFloorOutboundDetails);
                            if (wcsThreeFloorOutboundDetails != null) {
                                lineBodyLog.info("读码写入PLC：{},PLC地址位数据：{}", readCodeWritePlc, readHoldingRegisters);
                                if (readCodeWritePlc == 0 || (readCodeWritePlc == 1 && readHoldingRegisters == 0)) {
                                    //返回plc读码结果ok
                                    outboundLineBody.writeWithRetry(readCodeResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    boolean b3 = wcsLineBodyOutboundService.updateReadCodeWritePlc(wcsLineBodyOutboundListId, 1);

                                    //出库最后一段返回PLC往那个栈板放货
                                    Integer roboticArmRequestStackingStatus = outboundLineBody.readHoldingRegisters(roboticArmRequestStacking, unitId);
                                    lineBodyLog.info("机械臂1号请求放货,：{},获取到机械臂上的料箱号：{}", roboticArmRequestStackingStatus, container);
                                    if (roboticArmRequestStackingStatus == 1) {
                                        WcsThreeFloorOutboundDetails threeFloorOutboundDetails = wcsThreeFloorOutboundDetailsService.selectContainer(container);
                                        if (threeFloorOutboundDetails != null) {
                                            String workOrder = threeFloorOutboundDetails.getWorkOrder();
                                            //根据工单找到对应在栈板
                                            WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectWorkOrder(workOrder);
                                            lineBodyLog.info("根据工单号找到栈板：{}", wcsLineBodyPallet);
                                            if (wcsLineBodyPallet != null) {
                                                Integer id = threeFloorOutboundDetails.getId();
                                                Integer palletId = wcsLineBodyPallet.getPalletId();
                                                Integer lineBodyCode = wcsLineBodyPallet.getLineBodyCode();
                                                //判断当前容器是否绑定栈板
                                                if (threeFloorOutboundDetails.getPalletCode() == null) {
                                                    //通知plc放入该栈板
                                                    Integer workbinPlacePosition = wcsLineBodyOutbound.getWorkbinPlacePosition();
                                                    lineBodyLog.info("通知PLC请求放货位置：{}", lineBodyCode);
                                                    //发送plc，1号链条线
                                                    boolean b = outboundLineBody.writeWithRetry(workbinPlacePosition, lineBodyCode, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                                    Integer workbinPlacePositionStatus = outboundLineBody.readHoldingRegisters(workbinPlacePosition, unitId);
                                                    lineBodyLog.info("写入plc：{},写入plc数据：{}", b, workbinPlacePositionStatus);
                                                    if (b) {
                                                        //查询无机械手标记
                                                        WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails1 = wcsThreeFloorOutboundDetailsService.selectMechanicalArmLabel(TaskType.ROBOTIC_ARM_MARK.getStatus());
                                                        lineBodyLog.info("机械手查询是否有标记：{}", wcsThreeFloorOutboundDetails1);
                                                        if (wcsThreeFloorOutboundDetails1 == null) {
                                                            //记录标记一下机械臂上的料箱
                                                            boolean b4 = wcsThreeFloorOutboundDetailsService.updateMechanicalArmLabel(container, TaskType.ROBOTIC_ARM_MARK.getStatus());
                                                            lineBodyLog.info("记录标记机械臂1号需要放的栈板编号：{},料箱号：{}", lineBodyCode, container);
                                                        } else {
                                                            //机械臂请求，将不为0的标记，清楚
                                                            String containerCode = wcsThreeFloorOutboundDetails1.getContainerCode();
                                                            boolean b1 = wcsThreeFloorOutboundDetailsService.updateMechanicalArmLabel(containerCode, TaskType.ROBOTIC_ARM_DEFAULT.getStatus());
                                                            lineBodyLog.info("机械臂请求，将不为0的标记");
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            } else {
                                //当前容器码找不到信息，返回失败
                                outboundLineBody.writeWithRetry(readCodeWritePlc, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                boolean b3 = wcsLineBodyOutboundService.updateReadCodeWritePlc(wcsLineBodyOutboundListId, 1);
                            }
                        }
//                    outboundLineBody.readHoldingRegisters(readcode)

                    } else {
                        if (readCodeWritePlc == 1) {
                            //扫码储位没货标记修改为0
                            boolean b3 = wcsLineBodyOutboundService.updateReadCodeWritePlc(wcsLineBodyOutboundListId, 0);
                            //记录料箱被PLC拿走
                        }
                    }

                    //机械臂
//                Integer readHoldingRegistersStatus = outboundLineBody.readHoldingRegisters(roboticArmIsThereStock, unitId);
//                lineBodyLog.info("读取机械臂是否有货地址位：{},返回结果：{}", roboticArmIsThereStock, readHoldingRegistersStatus);
//                if (readHoldingRegistersStatus == 1) {
//                    //机械臂有料，读取料箱信息
//                    String boxCode = outboundLineBody.readHoldingRegistersString(roboticArmReadCode, unitId);
//                    if (boxCode != null) {
//                        //机械臂请求放入栈板上
//                        Integer roboticArmRequestStackingStatus = outboundLineBody.readHoldingRegisters(roboticArmRequestStacking, unitId);
//                        //                    Integer roboticArmRequestStackingTwoStatus = outboundLineBody.readHoldingRegisters(roboticArmRequestStackingTwo, unitId);
//                        lineBodyLog.info("机械臂1号请求放货,：{},获取到机械臂上的料箱号：{}", roboticArmRequestStackingStatus, boxCode);
//                        if (roboticArmRequestStackingStatus == 1) {
//                            WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = wcsThreeFloorOutboundDetailsService.selectContainer(boxCode);
//                            if (wcsThreeFloorOutboundDetails != null) {
//                                String workOrder = wcsThreeFloorOutboundDetails.getWorkOrder();
//                                //根据工单找到对应在栈板
//                                WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectWorkOrder(workOrder);
//                                lineBodyLog.info("根据工单号找到栈板：{}", wcsLineBodyPallet);
//                                if (wcsLineBodyPallet != null) {
//                                    Integer id = wcsThreeFloorOutboundDetails.getId();
//                                    Integer palletId = wcsLineBodyPallet.getPalletId();
//                                    Integer lineBodyCode = wcsLineBodyPallet.getLineBodyCode();
//                                    //判断当前容器是否绑定栈板
//                                    if (wcsThreeFloorOutboundDetails.getPalletCode() == null) {
//                                        //通知plc放入该栈板
//                                        Integer workbinPlacePosition = wcsLineBodyOutbound.getWorkbinPlacePosition();
//                                        lineBodyLog.info("通知PLC请求放货位置：{}", lineBodyCode);
//                                        //发送plc，1号链条线
//                                        boolean b = outboundLineBody.writeWithRetry(workbinPlacePosition, lineBodyCode, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                                        Integer workbinPlacePositionStatus = outboundLineBody.readHoldingRegisters(workbinPlacePosition, unitId);
//                                        lineBodyLog.info("写入plc：{},写入plc数据：{}", b, workbinPlacePositionStatus);
//                                        if (b) {
//                                            //查询无机械手标记
//                                            WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails1 = wcsThreeFloorOutboundDetailsService.selectMechanicalArmLabel(TaskType.ROBOTIC_ARM_MARK.getStatus());
//                                            lineBodyLog.info("机械手查询是否有标记：{}", wcsThreeFloorOutboundDetails1);
//                                            if (wcsThreeFloorOutboundDetails1 == null) {
//                                                //记录标记一下机械臂上的料箱
//                                                boolean b3 = wcsThreeFloorOutboundDetailsService.updateMechanicalArmLabel(boxCode, TaskType.ROBOTIC_ARM_MARK.getStatus());
//                                                lineBodyLog.info("记录标记机械臂1号需要放的栈板编号：{},料箱号：{}", lineBodyCode, boxCode);
//                                            } else {
//                                                //机械臂请求，将不为0的标记，清楚
//                                                String containerCode = wcsThreeFloorOutboundDetails1.getContainerCode();
//                                                boolean b1 = wcsThreeFloorOutboundDetailsService.updateMechanicalArmLabel(containerCode, TaskType.ROBOTIC_ARM_DEFAULT.getStatus());
//                                                lineBodyLog.info("机械臂请求，将不为0的标记");
//                                            }
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }

                    //请求栈板
                    Integer oneTeturnName = wcsLineBodyOutbound.getOneTeturnName();
                    Integer twoTeturnName = wcsLineBodyOutbound.getTwoTeturnName();
                    Integer oneRequestPalletCodeName = wcsLineBodyOutbound.getOneRequestPalletCodeName();
                    Integer readHoldingRegisters = outboundLineBody.readHoldingRegisters(oneRequestPalletCodeName, unitId);
                    Integer moveStatus = outboundLineBody.readHoldingRegisters(1, unitId);
                    if (readHoldingRegisters == 1 && moveStatus == 2) {
                        lineBodyLog.info("第一段plc请求栈板数据：{},地址位:{}", readHoldingRegisters, oneRequestPalletCodeName);
                        //请求栈板
                        Integer count = wcsLineBodyPalletService.selectCount();
                        if (count < 2) {
                            //要有出库单
                            List<WcsThreeFloorOutbound> wcsThreeFloorOutbounds = wcsThreeFloorOutboundService.selectNoMechanicalArmCompleteList(0);
                            if (!wcsThreeFloorOutbounds.isEmpty()) {
                                List<WcsLineBodyPallet> wcsLineBodyPalletList = wcsLineBodyPalletService.selectList();
                                // 提取 workOrder 字段，组成 Set
                                Set<String> existWorkOrders = wcsLineBodyPalletList.stream()
                                        .map(WcsLineBodyPallet::getWorkOrder)
                                        .collect(Collectors.toSet());

                                // 过滤掉已存在的工单
                                List<String> workOrderList = wcsThreeFloorOutbounds.stream()
                                        .map(WcsThreeFloorOutbound::getWorkOrder)
                                        .filter(s -> !existWorkOrders.contains(s))
                                        .collect(Collectors.toList());
                                //                        List<String> workOrderList = wcsThreeFloorOutbounds.stream().map(WcsThreeFloorOutbound::getWorkOrder).filter(s -> !wcsLineBodyPalletList.contains(s)).collect(Collectors.toList());
                                lineBodyLog.info("查询线体栈板：{},过滤后数据：{}", wcsLineBodyPalletList, workOrderList);
                                if (!workOrderList.isEmpty()) {
                                    String workOrder = workOrderList.get(0);
                                    lineBodyLog.info("第一段请求查询工单号：{}", workOrder);

                                    synchronized (LockConfig.objectLock) {
                                        //创建线体栈板
                                        WcsLineBodyPallet wcsLineBodyPallet = new WcsLineBodyPallet();
                                        WcsConfiguration wcsConfiguration = wcsConfigurationService.selectKey(TaskType.THREE_FLOOR_PALLET.getMessage());
                                        if (wcsConfiguration != null) {
                                            String configValue = wcsConfiguration.getConfigValue();
                                            Integer palletId = Integer.valueOf(configValue);
                                            if (palletId == 1000) {
                                                palletId = 0;
                                                //                                        boolean b = wcsConfigurationService.setValue(TaskType.THREE_FLOOR_PALLET.getMessage(),palletId);
                                                //                                        if (!b){
                                                //                                            lineBodyLog.error("数据库异常,修改3楼出库栈板号异常");
                                                //                                            throw new RuntimeException("数据库异常,修改3楼出库栈板号异常");
                                                //                                        }
                                            }
                                            palletId++;
                                            wcsLineBodyPallet.setPalletId(palletId);
                                            wcsLineBodyPallet.setLineBodyCode(1);
                                            wcsLineBodyPallet.setPalletQuantity(0);
                                            wcsLineBodyPallet.setWorkOrder(workOrder);
                                            lineBodyLog.info("第一段创建栈板数据：{}", wcsLineBodyPallet);
                                            boolean b = wcsLineBodyPalletService.insert(wcsLineBodyPallet);
                                            if (!b) {
                                                lineBodyLog.error("创建线体栈板失败，数据异常");
                                                throw new RuntimeException("创建线体栈板失败，数据异常");
                                            }
                                            //修改栈板数量
                                            boolean b2 = wcsConfigurationService.setValue(TaskType.THREE_FLOOR_PALLET.getMessage(), palletId);
                                            if (!b2) {
                                                lineBodyLog.error("数据库异常,修改3楼出库栈板号异常");
                                                throw new RuntimeException("数据库异常,修改3楼出库栈板号异常");
                                            }
                                            outboundLineBody.writeWithRetry(oneTeturnName, palletId, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                            outboundLineBody.writeWithRetry(600, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                        }
                                    }

                                }
                            }
                        }
                    }
                    Integer twoRequestPalletCodeName = wcsLineBodyOutbound.getTwoRequestPalletCodeName();
                    Integer holdingRegisters = outboundLineBody.readHoldingRegisters(twoRequestPalletCodeName, unitId);
                    if (holdingRegisters != null) {

                    }
                    if (holdingRegisters == 1 && moveStatus == 2) {
                        lineBodyLog.info("第二段plc请求栈板数据：{},地址位：{}", holdingRegisters, twoRequestPalletCodeName);
                        //请求栈板
                        Integer count1 = wcsLineBodyPalletService.selectCount();
                        if (count1 < 2) {
                            //要有出库单
                            List<WcsThreeFloorOutbound> wcsThreeFloorOutbounds = wcsThreeFloorOutboundService.selectNoMechanicalArmCompleteList(0);
                            if (!wcsThreeFloorOutbounds.isEmpty()) {
                                List<WcsLineBodyPallet> wcsLineBodyPalletList = wcsLineBodyPalletService.selectList();
                                List<String> workOrderList = wcsThreeFloorOutbounds.stream().map(WcsThreeFloorOutbound::getWorkOrder).collect(Collectors.toList());
                                List<String> collect = wcsLineBodyPalletList.stream().map(WcsLineBodyPallet::getWorkOrder).collect(Collectors.toList());
                                workOrderList.removeAll(collect);
                                //                        List<String> workOrderList = wcsThreeFloorOutbounds.stream().map(WcsThreeFloorOutbound::getWorkOrder).filter(s -> !wcsLineBodyPalletList.contains(s)).collect(Collectors.toList());
                                lineBodyLog.info("获取到未分配的工单：{}", workOrderList);
                                if (!workOrderList.isEmpty()) {
                                    String workOrder = workOrderList.get(0);
                                    lineBodyLog.info("第二段请求查询工单号：{}", workOrder);

                                    synchronized (LockConfig.objectLock) {
                                        //创建线体栈板
                                        WcsLineBodyPallet wcsLineBodyPallet = new WcsLineBodyPallet();
                                        WcsConfiguration wcsConfiguration = wcsConfigurationService.selectKey(TaskType.THREE_FLOOR_PALLET.getMessage());
                                        if (wcsConfiguration != null) {
                                            String configValue = wcsConfiguration.getConfigValue();
                                            Integer palletId = Integer.valueOf(configValue);
                                            if (palletId == 1000) {
                                                palletId = 0;
                                                //                                        boolean b = wcsConfigurationService.setValue(TaskType.THREE_FLOOR_PALLET.getMessage(),palletId);
                                                //                                        if (!b){
                                                //                                            lineBodyLog.error("数据库异常,修改3楼出库栈板号异常");
                                                //                                            throw new RuntimeException("数据库异常,修改3楼出库栈板号异常");
                                                //                                        }
                                            }
                                            palletId++;
                                            wcsLineBodyPallet.setPalletId(palletId);
                                            wcsLineBodyPallet.setLineBodyCode(2);
                                            wcsLineBodyPallet.setPalletQuantity(0);
                                            wcsLineBodyPallet.setWorkOrder(workOrder);
                                            lineBodyLog.info("第二段创建栈板数据：{}", wcsLineBodyPallet);
                                            boolean b = wcsLineBodyPalletService.insert(wcsLineBodyPallet);
                                            if (!b) {
                                                lineBodyLog.error("创建线体栈板失败，数据异常");
                                                throw new RuntimeException("创建线体栈板失败，数据异常");
                                            }
                                            //修改栈板数量
                                            boolean b2 = wcsConfigurationService.setValue(TaskType.THREE_FLOOR_PALLET.getMessage(), palletId);
                                            if (!b2) {
                                                lineBodyLog.error("数据库异常,修改3楼出库栈板号异常");
                                                throw new RuntimeException("数据库异常,修改3楼出库栈板号异常");
                                            }
                                            outboundLineBody.writeWithRetry(twoTeturnName, palletId, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                            outboundLineBody.writeWithRetry(601, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                        }
                                    }


                                }
                            }
                        }
                    }

                    //plc放货，返回是否需要往下流
                    Integer oneChainBelow = wcsLineBodyOutbound.getOneChainBelow();
                    Integer twoChainBelow = wcsLineBodyOutbound.getTwoChainBelow();
                    Integer oneChainPalletCode = wcsLineBodyOutbound.getOneChainPalletCode();
                    Integer fourChainPalletCode = wcsLineBodyOutbound.getFourChainPalletCode();
                    Integer oneChainBelowResult = wcsLineBodyOutbound.getOneChainBelowResult();
                    Integer twoChainBelowResult = wcsLineBodyOutbound.getTwoChainBelowResult();
                    Integer onePalletSize = wcsLineBodyOutbound.getOnePalletSize();
                    Integer twoPalletSize = wcsLineBodyOutbound.getTwoPalletSize();
                    //第一段链条机栈板号
                    Integer onePalletCode = outboundLineBody.readHoldingRegisters(oneChainPalletCode, unitId);
                    WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectPalletId(onePalletCode);
                    //第一段向下流
                    Integer oneObsceneStatus = outboundLineBody.readHoldingRegisters(oneChainBelow, unitId);
                    if (oneObsceneStatus == 1) {
                        //料箱数量
                        Integer boxSize = outboundLineBody.readHoldingRegisters(1021, unitId);
                        lineBodyLog.info("PLC数量：{}", boxSize);
                        if (boxSize == 16) {
                            lineBodyLog.info("满16个通知plc往前流,PLC,boxSize:{}",boxSize);
                            if (wcsLineBodyPallet != null) {
                                String workOrder = wcsLineBodyPallet.getWorkOrder();
                                WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                int id = wcsThreeFloorOutbound.getId();
                                Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
                                Integer total = wcsThreeFloorOutbound.getTotal();
                                completeNumber = completeNumber + boxSize;
                                lineBodyLog.info("3楼更新更新出库数量：{}", completeNumber);
                                //更新出库单，更新完成数
                                boolean b1 = wcsThreeFloorOutboundService.updateComplete(id, completeNumber);
                                if (Objects.equals(total, completeNumber)) {
                                    //修改当前出库单完成
                                    wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                                }
                                //将栈板移到历史表
                                WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                                BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
                                wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                                //删除栈板信息
                                wcsLineBodyPalletService.deleteId(onePalletCode);
                                Thread.sleep(1);
                                //修改明细表，记录栈板上的数量
                                for (Integer i = 1; i <= boxSize; i++) {
                                    switch (i) {
                                        case 1:
                                            String box1 = outboundLineBody.readHoldingRegistersString10(1040, unitId);
                                            lineBodyLog.info("满班，第一箱：{}",box1);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box1, onePalletCode);
                                            break;
                                        case 2:
                                            String box2 = outboundLineBody.readHoldingRegistersString10(1050, unitId);
                                            lineBodyLog.info("满班，第二箱：{}",box2);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box2, onePalletCode);
                                            break;
                                        case 3:
                                            String box3 = outboundLineBody.readHoldingRegistersString10(1060, unitId);
                                            lineBodyLog.info("满班，第三箱：{}",box3);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box3, onePalletCode);
                                            break;
                                        case 4:
                                            String box4 = outboundLineBody.readHoldingRegistersString10(1070, unitId);
                                            lineBodyLog.info("满班，第四箱：{}",box4);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box4, onePalletCode);
                                            break;
                                        case 5:
                                            String box5 = outboundLineBody.readHoldingRegistersString10(1080, unitId);
                                            lineBodyLog.info("满班，第五箱：{}",box5);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box5, onePalletCode);
                                            break;
                                        case 6:
                                            String box6 = outboundLineBody.readHoldingRegistersString10(1090, unitId);
                                            lineBodyLog.info("满班，第六箱：{}",box6);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box6, onePalletCode);
                                            break;
                                        case 7:
                                            String box7 = outboundLineBody.readHoldingRegistersString10(1100, unitId);
                                            lineBodyLog.info("满班，第七箱：{}",box7);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box7, onePalletCode);
                                            break;
                                        case 8:
                                            String box8 = outboundLineBody.readHoldingRegistersString10(1110, unitId);
                                            lineBodyLog.info("满班，第八箱：{}",box8);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box8, onePalletCode);
                                            break;
                                        case 9:
                                            String box9 = outboundLineBody.readHoldingRegistersString10(1120, unitId);
                                            lineBodyLog.info("满班，第九箱：{}",box9);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box9, onePalletCode);
                                            break;
                                        case 10:
                                            String box10 = outboundLineBody.readHoldingRegistersString10(1130, unitId);
                                            lineBodyLog.info("满班，第十箱：{}",box10);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box10, onePalletCode);
                                            break;
                                        case 11:
                                            String box11 = outboundLineBody.readHoldingRegistersString10(1140, unitId);
                                            lineBodyLog.info("满班，第十一箱：{}",box11);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box11, onePalletCode);
                                            break;
                                        case 12:
                                            String box12 = outboundLineBody.readHoldingRegistersString10(1150, unitId);
                                            lineBodyLog.info("满班，第十二箱：{}",box12);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box12, onePalletCode);
                                            break;
                                        case 13:
                                            String box13 = outboundLineBody.readHoldingRegistersString10(1160, unitId);
                                            lineBodyLog.info("满班，第十三箱：{}",box13);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box13, onePalletCode);
                                            break;
                                        case 14:
                                            String box14 = outboundLineBody.readHoldingRegistersString10(1170, unitId);
                                            lineBodyLog.info("满班，第十四箱：{}",box14);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box14, onePalletCode);
                                            break;
                                        case 15:
                                            String box15 = outboundLineBody.readHoldingRegistersString10(1180, unitId);
                                            lineBodyLog.info("满班，第十五箱：{}",box15);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box15, onePalletCode);
                                            break;
                                        case 16:
                                            String box16 = outboundLineBody.readHoldingRegistersString10(1190, unitId);
                                            lineBodyLog.info("满班，第十六箱：{}",box16);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box16, onePalletCode);
                                            break;
                                    }
                                }
                                boolean b = outboundLineBody.writeWithRetry(oneChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                            }
                        } else {
                            if (wcsLineBodyPallet != null) {
                                String workOrder = wcsLineBodyPallet.getWorkOrder();
                                WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                if (wcsThreeFloorOutbound != null) {
                                    Integer total = wcsThreeFloorOutbound.getTotal();
                                    Integer completeNumber1 = wcsThreeFloorOutbound.getCompleteNumber();
                                    int remainingQuantity;
                                    remainingQuantity = total - completeNumber1;
                                    lineBodyLog.info("剩余未完成：{}，PLC数量:{},总数：{}", remainingQuantity, boxSize, total);
                                    if (Objects.equals(total, boxSize)) {
                                        lineBodyLog.info("第一段链条机总数等于PLC数量：{}，{}", total, boxSize);
                                        WcsThreeFloorOutbound wcsThreeFloorOutbound1 = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                        int id = wcsThreeFloorOutbound1.getId();
                                        Integer completeNumber = wcsThreeFloorOutbound1.getCompleteNumber();
                                        completeNumber = completeNumber + boxSize;
                                        lineBodyLog.info("3楼更新更新出库数量：{}", completeNumber);
                                        //更新出库单，更新完成数
                                        boolean b1 = wcsThreeFloorOutboundService.updateComplete(id, completeNumber);
                                        if (Objects.equals(total, completeNumber)) {
                                            //修改当前出库单完成
                                            wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                                        }
                                        //将栈板移到历史表
                                        WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                                        BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
                                        wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                                        //删除栈板信息
                                        wcsLineBodyPalletService.deleteId(onePalletCode);
                                        Thread.sleep(1);
                                        for (Integer i = 1; i <= boxSize; i++) {
                                            switch (i) {
                                                case 1:
                                                    String box1 = outboundLineBody.readHoldingRegistersString10(1040, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第一箱：{}",box1);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box1, onePalletCode);
                                                    break;
                                                case 2:
                                                    String box2 = outboundLineBody.readHoldingRegistersString10(1050, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第二箱：{}",box2);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box2, onePalletCode);
                                                    break;
                                                case 3:
                                                    String box3 = outboundLineBody.readHoldingRegistersString10(1060, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第三箱：{}",box3);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box3, onePalletCode);
                                                    break;
                                                case 4:
                                                    String box4 = outboundLineBody.readHoldingRegistersString10(1070, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第四箱：{}",box4);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box4, onePalletCode);
                                                    break;
                                                case 5:
                                                    String box5 = outboundLineBody.readHoldingRegistersString10(1080, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第五箱：{}",box5);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box5, onePalletCode);
                                                    break;
                                                case 6:
                                                    String box6 = outboundLineBody.readHoldingRegistersString10(1090, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第六箱：{}",box6);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box6, onePalletCode);
                                                    break;
                                                case 7:
                                                    String box7 = outboundLineBody.readHoldingRegistersString10(1100, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第七箱：{}",box7);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box7, onePalletCode);
                                                    break;
                                                case 8:
                                                    String box8 = outboundLineBody.readHoldingRegistersString10(1110, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第八箱：{}",box8);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box8, onePalletCode);
                                                    break;
                                                case 9:
                                                    String box9 = outboundLineBody.readHoldingRegistersString10(1120, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第九箱：{}",box9);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box9, onePalletCode);
                                                    break;
                                                case 10:
                                                    String box10 = outboundLineBody.readHoldingRegistersString10(1130, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第十箱：{}",box10);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box10, onePalletCode);
                                                    break;
                                                case 11:
                                                    String box11 = outboundLineBody.readHoldingRegistersString10(1140, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第十一箱：{}",box11);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box11, onePalletCode);
                                                    break;
                                                case 12:
                                                    String box12 = outboundLineBody.readHoldingRegistersString10(1150, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第十二箱：{}",box12);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box12, onePalletCode);
                                                    break;
                                                case 13:
                                                    String box13 = outboundLineBody.readHoldingRegistersString10(1160, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第十三箱：{}",box13);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box13, onePalletCode);
                                                    break;
                                                case 14:
                                                    String box14 = outboundLineBody.readHoldingRegistersString10(1170, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第十四箱：{}",box14);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box14, onePalletCode);
                                                    break;
                                                case 15:
                                                    String box15 = outboundLineBody.readHoldingRegistersString10(1180, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第十五箱：{}",box15);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box15, onePalletCode);
                                                    break;
                                                case 16:
                                                    String box16 = outboundLineBody.readHoldingRegistersString10(1190, unitId);
                                                    lineBodyLog.info("出库单与总数相同，第十六箱：{}",box16);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box16, onePalletCode);
                                                    break;
                                            }
                                        }
                                        boolean b = outboundLineBody.writeWithRetry(oneChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    } else if (Objects.equals(remainingQuantity, boxSize)) {
                                        lineBodyLog.info("第一段通知plc往前流，剩余数量：{},PLC数量：{}", remainingQuantity, boxSize);
                                        WcsThreeFloorOutbound wcsThreeFloorOutbound1 = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                        int id = wcsThreeFloorOutbound1.getId();
                                        Integer completeNumber = wcsThreeFloorOutbound1.getCompleteNumber();
                                        completeNumber = completeNumber + boxSize;
                                        lineBodyLog.info("3楼更新更新出库数量：{}", completeNumber);
                                        //更新出库单，更新完成数
                                        boolean b1 = wcsThreeFloorOutboundService.updateComplete(id, completeNumber);
                                        if (Objects.equals(total, completeNumber)) {
                                            //修改当前出库单完成
                                            wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                                        }
                                        //将栈板移到历史表
                                        WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                                        BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
                                        wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                                        //删除栈板信息
                                        wcsLineBodyPalletService.deleteId(onePalletCode);
                                        Thread.sleep(1);
                                        for (Integer i = 1; i <= boxSize; i++) {
                                            switch (i) {
                                                case 1:
                                                    String box1 = outboundLineBody.readHoldingRegistersString10(1040, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第一箱：{}",box1);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box1, onePalletCode);
                                                    break;
                                                case 2:
                                                    String box2 = outboundLineBody.readHoldingRegistersString10(1050, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第二箱：{}",box2);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box2, onePalletCode);
                                                    break;
                                                case 3:
                                                    String box3 = outboundLineBody.readHoldingRegistersString10(1060, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第三箱：{}",box3);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box3, onePalletCode);
                                                    break;
                                                case 4:
                                                    String box4 = outboundLineBody.readHoldingRegistersString10(1070, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第四箱：{}",box4);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box4, onePalletCode);
                                                    break;
                                                case 5:
                                                    String box5 = outboundLineBody.readHoldingRegistersString10(1080, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第五箱：{}",box5);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box5, onePalletCode);
                                                    break;
                                                case 6:
                                                    String box6 = outboundLineBody.readHoldingRegistersString10(1090, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第六箱：{}",box6);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box6, onePalletCode);
                                                    break;
                                                case 7:
                                                    String box7 = outboundLineBody.readHoldingRegistersString10(1100, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第七箱：{}",box7);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box7, onePalletCode);
                                                    break;
                                                case 8:
                                                    String box8 = outboundLineBody.readHoldingRegistersString10(1110, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第八箱：{}",box8);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box8, onePalletCode);
                                                    break;
                                                case 9:
                                                    String box9 = outboundLineBody.readHoldingRegistersString10(1120, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第九箱：{}",box9);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box9, onePalletCode);
                                                    break;
                                                case 10:
                                                    String box10 = outboundLineBody.readHoldingRegistersString10(1130, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第十箱：{}",box10);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box10, onePalletCode);
                                                    break;
                                                case 11:
                                                    String box11 = outboundLineBody.readHoldingRegistersString10(1140, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第十一箱：{}",box11);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box11, onePalletCode);
                                                    break;
                                                case 12:
                                                    String box12 = outboundLineBody.readHoldingRegistersString10(1150, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第十二箱：{}",box12);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box12, onePalletCode);
                                                    break;
                                                case 13:
                                                    String box13 = outboundLineBody.readHoldingRegistersString10(1160, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第十三箱：{}",box13);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box13, onePalletCode);
                                                    break;
                                                case 14:
                                                    String box14 = outboundLineBody.readHoldingRegistersString10(1170, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第十四箱：{}",box14);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box14, onePalletCode);
                                                    break;
                                                case 15:
                                                    String box15 = outboundLineBody.readHoldingRegistersString10(1180, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第十五箱：{}",box15);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box15, onePalletCode);
                                                    break;
                                                case 16:
                                                    String box16 = outboundLineBody.readHoldingRegistersString10(1190, unitId);
                                                    lineBodyLog.info("出库单剩余数量与PLC数相同，第十六箱：{}",box16);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box16, onePalletCode);
                                                    break;
                                            }
                                        }
                                        boolean b = outboundLineBody.writeWithRetry(oneChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    } else {
                                        //通知PLC不允许往下流
                                        lineBodyLog.info("1号线体通知plc继续码垛");
                                        boolean b = outboundLineBody.writeWithRetry(oneChainBelowResult, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    }
                                }
                            }
                        }


//                    lineBodyLog.info("第一段向下流");
//                    //查询被机械臂标记的料箱
//                    WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = wcsThreeFloorOutboundDetailsService.selectMechanicalArmLabel(TaskType.ROBOTIC_ARM_MARK.getStatus());
//                    if (wcsThreeFloorOutboundDetails != null) {
//                        //将料箱记录到栈板上
//                        String containerCode = wcsThreeFloorOutboundDetails.getContainerCode();
//                        String workOrder = wcsThreeFloorOutboundDetails.getWorkOrder();
//                        boolean b = wcsThreeFloorOutboundDetailsService.updateMechanicalArmLabel(containerCode, TaskType.ROBOTIC_ARM_DEFAULT.getStatus());
//                        WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectWorkOrder(workOrder);
//                        lineBodyLog.info("是否需要往下流根据工单号找到栈板：{}", wcsLineBodyPallet);
//                        if (wcsLineBodyPallet != null) {
//                            Integer id = wcsThreeFloorOutboundDetails.getId();
//                            Integer palletId = wcsLineBodyPallet.getPalletId();
//                            Integer lineBodyCode = wcsLineBodyPallet.getLineBodyCode();
//                            lineBodyLog.info("通知plc将料箱放入链条线：{}", lineBodyCode);
//                            //栈板表+1
//                            Integer palletQuantity;
//                            palletQuantity = wcsLineBodyPallet.getPalletQuantity();
//                            palletQuantity++;
//                            //更新栈板数量
//                            lineBodyLog.info("更新栈板数量,栈板号：{}，数量：{}", palletId, palletQuantity);
//                            boolean b1 = wcsLineBodyPalletService.updatePalletQuantity(palletId, palletQuantity);
//                            if (!b1) {
//                                lineBodyLog.error("更新栈板数量失败");
//                                throw new RuntimeException("更新栈板数量失败");
//                            }
//                            //记录当前容器所在栈板
//                            boolean b2 = wcsThreeFloorOutboundDetailsService.updatePallerCode(id, palletId);
//                            if (!b2) {
//                                lineBodyLog.error("3楼出库单明细记录所在栈板失败");
//                                throw new RuntimeException("3楼出库单明细记录所在栈板失败");
//                            }
//                            //工单号完成数+1
//                            WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                            if (wcsThreeFloorOutbound != null) {
//                                Integer completeNumber;
//                                completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
//                                completeNumber++;
//                                boolean b4 = wcsThreeFloorOutboundService.updateComplete(wcsThreeFloorOutbound.getId(), completeNumber);
//                                if (!b4) {
//                                    lineBodyLog.error("3楼出库单记录栈板数+1失败");
//                                    throw new RuntimeException("3楼出库单记录栈板数+1失败");
//                                }
//                            }
//
//                        }
//                    }
//
//                    //第一段是否允许往下流
//                    Integer onePalletCode = outboundLineBody.readHoldingRegisters(oneChainPalletCode, unitId);
//                    lineBodyLog.info("第一段是否允许往下流：{}", onePalletCode);
//                    WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectPalletId(onePalletCode);
//                    if (wcsLineBodyPallet != null) {
//                        Integer onePlcCount = outboundLineBody.readHoldingRegisters(onePalletSize, unitId);
//                        Integer palletQuantity = wcsLineBodyPallet.getPalletQuantity();
//                        String workOrder = wcsLineBodyPallet.getWorkOrder();
//                        Integer palletId = wcsLineBodyPallet.getPalletId();
//                        Integer palletSize = 16;
//                        WcsConfiguration wcsConfiguration = wcsConfigurationService.selectKey(TaskType.THREE_FLOOR_OUTBOUND_PALLET_QUANTITY.getMessage());
//                        if (wcsConfiguration != null) {
//                            palletSize = Integer.valueOf(wcsConfiguration.getConfigValue());
//                        }
//                        lineBodyLog.info("栈板数量：{}，一块栈板最多放置数量：{},plc数量：{}", palletQuantity, palletSize, onePlcCount);
//                        if (Objects.equals(palletQuantity, palletSize) && Objects.equals(palletQuantity, onePlcCount)) {
//                            //通知plc往前流
//                            lineBodyLog.info("通知plc往前流");
//                            boolean b = outboundLineBody.writeWithRetry(oneChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                            if (!b) {
//                                lineBodyLog.error("通知plc第一段链条线往下流失败");
//                                throw new RuntimeException("通知plc第一段链条线往下流失败");
//                            }
//                            //将栈板移到历史表
//                            WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
//                            BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
//                            boolean b1 = wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
//                            //删除栈板信息
//                            boolean b2 = wcsLineBodyPalletService.deleteId(palletId);
//                            //查询出库单都完成了修改状态为1
//                            WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                            if (wcsThreeFloorOutbound != null) {
//                                int id = wcsThreeFloorOutbound.getId();
//                                Integer total = wcsThreeFloorOutbound.getTotal();
//                                Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
//                                if (Objects.equals(total, completeNumber)) {
//                                    //修改出库单，机械臂状态
//                                    wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
//                                }
//                            }
//                        }
//                        else {
//                            //未满栈板数量，判断剩余数量+栈板数量能不能凑一版，能不出，不能将所有料箱到栈板上才出
//                            WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                            lineBodyLog.info("未满板,工单号：{},查询数据：{}", workOrder, wcsThreeFloorOutbound);
//                            if (wcsThreeFloorOutbound != null) {
//                                Integer total = wcsThreeFloorOutbound.getTotal();
//                                Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
//                                Integer remainingQuantity;
//                                remainingQuantity = total - completeNumber;
//                                lineBodyLog.info("剩余数量：{},一块栈板最多数量：{}", remainingQuantity, palletSize);
//                                if (remainingQuantity > palletSize || (remainingQuantity < palletSize && remainingQuantity != 0)) {
//                                    //通知plc不往下流
//                                    lineBodyLog.info("通知plc不往下流");
//                                    boolean b = outboundLineBody.writeWithRetry(oneChainBelowResult, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                                } else if (remainingQuantity == 0) {
//                                    //通知plc往前流
//                                    lineBodyLog.info("通知plc往前流");
//                                    boolean b = outboundLineBody.writeWithRetry(oneChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                                    if (!b) {
//                                        lineBodyLog.error("通知plc第一段链条线往下流失败");
//                                        throw new RuntimeException("通知plc第一段链条线往下流失败");
//                                    }
//                                    //将栈板移到历史表
//                                    WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
//                                    BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
//                                    boolean b1 = wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
//                                    //删除栈板信息
//                                    boolean b2 = wcsLineBodyPalletService.deleteId(palletId);
//
//                                    WcsThreeFloorOutbound threeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                                    if (threeFloorOutbound != null) {
//                                        int id = threeFloorOutbound.getId();
//                                        Integer totals = threeFloorOutbound.getTotal();
//                                        Integer completeNumbers = threeFloorOutbound.getCompleteNumber();
//                                        if (Objects.equals(totals, completeNumbers)) {
//                                            //修改出库单，机械臂状态
//                                            wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
//                                        }
//                                    }
//
//                                }
//                            }
//                        }
//                    }
                    }

                    //第四段向下流
                    Integer twoObsceneStatus = outboundLineBody.readHoldingRegisters(twoChainBelow, unitId);
                    Integer fourPalletCode = outboundLineBody.readHoldingRegisters(fourChainPalletCode, unitId);
                    lineBodyLog.info("第四条栈板号：{},往下流请求：{}", fourPalletCode, twoObsceneStatus);
                    WcsLineBodyPallet wcsLineBodyPalletFour = wcsLineBodyPalletService.selectPalletId(fourPalletCode);
                    if (twoObsceneStatus == 1) {
                        //料箱数量
                        Integer boxSize = outboundLineBody.readHoldingRegisters(1621, unitId);
                        lineBodyLog.info("第四段向下流PLC数量：{}", boxSize);
                        if (boxSize == 16) {
                            lineBodyLog.info("通知plc往前流");
                            if (wcsLineBodyPalletFour != null) {
                                String workOrder = wcsLineBodyPalletFour.getWorkOrder();
                                WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                int id = wcsThreeFloorOutbound.getId();
                                Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
                                Integer total = wcsThreeFloorOutbound.getTotal();
                                completeNumber = completeNumber + boxSize;
                                lineBodyLog.info("3楼更新更新出库数量：{}", completeNumber);
                                //更新出库单，更新完成数
                                boolean b1 = wcsThreeFloorOutboundService.updateComplete(id, completeNumber);
                                if (Objects.equals(total, completeNumber)) {
                                    //修改当前出库单完成
                                    wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                                }
                                //将栈板移到历史表
                                WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                                BeanUtils.copyProperties(wcsLineBodyPalletFour, wcsLineBodyPalletHistory);
                                wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                                //删除栈板信息
                                wcsLineBodyPalletService.deleteId(fourPalletCode);
                                Thread.sleep(1);
                                //修改明细表，记录栈板上的数量
                                for (Integer i = 1; i <= boxSize; i++) {
                                    switch (i) {
                                        case 1:
                                            String box1 = outboundLineBody.readHoldingRegistersString10(1640, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第一箱：{}",box1);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box1, fourPalletCode);
                                            break;
                                        case 2:
                                            String box2 = outboundLineBody.readHoldingRegistersString10(1650, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第二箱：{}",box2);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box2, fourPalletCode);
                                            break;
                                        case 3:
                                            String box3 = outboundLineBody.readHoldingRegistersString10(1660, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第三箱：{}",box3);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box3, fourPalletCode);
                                            break;
                                        case 4:
                                            String box4 = outboundLineBody.readHoldingRegistersString10(1670, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第四箱：{}",box4);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box4, fourPalletCode);
                                            break;
                                        case 5:
                                            String box5 = outboundLineBody.readHoldingRegistersString10(1680, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第五箱：{}",box5);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box5, fourPalletCode);
                                            break;
                                        case 6:
                                            String box6 = outboundLineBody.readHoldingRegistersString10(1690, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第六箱：{}",box6);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box6, fourPalletCode);
                                            break;
                                        case 7:
                                            String box7 = outboundLineBody.readHoldingRegistersString10(1700, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第七箱：{}",box7);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box7, fourPalletCode);
                                            break;
                                        case 8:
                                            String box8 = outboundLineBody.readHoldingRegistersString10(1710, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第八箱：{}",box8);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box8, fourPalletCode);
                                            break;
                                        case 9:
                                            String box9 = outboundLineBody.readHoldingRegistersString10(1720, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第九箱：{}",box9);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box9, fourPalletCode);
                                            break;
                                        case 10:
                                            String box10 = outboundLineBody.readHoldingRegistersString10(1730, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第十箱：{}",box10);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box10, fourPalletCode);
                                            break;
                                        case 11:
                                            String box11 = outboundLineBody.readHoldingRegistersString10(1740, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第十一箱：{}",box11);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box11, fourPalletCode);
                                            break;
                                        case 12:
                                            String box12 = outboundLineBody.readHoldingRegistersString10(1750, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第十二箱：{}",box12);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box12, fourPalletCode);
                                            break;
                                        case 13:
                                            String box13 = outboundLineBody.readHoldingRegistersString(1760, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第十三箱：{}",box13);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box13, fourPalletCode);
                                            break;
                                        case 14:
                                            String box14 = outboundLineBody.readHoldingRegistersString10(1770, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第十四箱：{}",box14);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box14, fourPalletCode);
                                            break;
                                        case 15:
                                            String box15 = outboundLineBody.readHoldingRegistersString10(1780, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第十五箱：{}",box15);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box15, fourPalletCode);
                                            break;
                                        case 16:
                                            String box16 = outboundLineBody.readHoldingRegistersString10(1790, unitId);
                                            lineBodyLog.info("满版,第四段向下流，第十六箱：{}",box16);
                                            wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box16, fourPalletCode);
                                            break;
                                    }
                                }
                                boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                            }
                        } else {
                            if (wcsLineBodyPalletFour != null) {
                                String workOrder = wcsLineBodyPalletFour.getWorkOrder();
                                WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                if (wcsThreeFloorOutbound != null) {
                                    Integer total = wcsThreeFloorOutbound.getTotal();
                                    Integer completeNumber1 = wcsThreeFloorOutbound.getCompleteNumber();
                                    int remainingQuantity;
                                    remainingQuantity = total - completeNumber1;
                                    lineBodyLog.info("第四段总数：{}，PLC数量:{}", total, boxSize);
                                    if (Objects.equals(total, boxSize)) {
                                        lineBodyLog.info("通知plc往前流");
                                        WcsThreeFloorOutbound wcsThreeFloorOutbound1 = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                        int id = wcsThreeFloorOutbound1.getId();
                                        Integer completeNumber = wcsThreeFloorOutbound1.getCompleteNumber();
                                        completeNumber = completeNumber + boxSize;
                                        lineBodyLog.info("3楼更新更新出库数量：{}", completeNumber);
                                        //更新出库单，更新完成数
                                        boolean b1 = wcsThreeFloorOutboundService.updateComplete(id, completeNumber);
                                        if (Objects.equals(total, completeNumber)) {
                                            //修改当前出库单完成
                                            wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                                        }
                                        //将栈板移到历史表
                                        WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                                        BeanUtils.copyProperties(wcsLineBodyPalletFour, wcsLineBodyPalletHistory);
                                        wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                                        //删除栈板信息
                                        wcsLineBodyPalletService.deleteId(fourPalletCode);
                                        Thread.sleep(1);
                                        for (Integer i = 1; i <= boxSize; i++) {
                                            switch (i) {
                                                case 1:
                                                    String box1 = outboundLineBody.readHoldingRegistersString10(1640, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第一箱：{}",box1);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box1, fourPalletCode);
                                                    break;
                                                case 2:
                                                    String box2 = outboundLineBody.readHoldingRegistersString10(1650, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第二箱：{}",box2);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box2, fourPalletCode);
                                                    break;
                                                case 3:
                                                    String box3 = outboundLineBody.readHoldingRegistersString10(1660, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第三箱：{}",box3);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box3, fourPalletCode);
                                                    break;
                                                case 4:
                                                    String box4 = outboundLineBody.readHoldingRegistersString10(1670, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第四箱：{}",box4);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box4, fourPalletCode);
                                                    break;
                                                case 5:
                                                    String box5 = outboundLineBody.readHoldingRegistersString10(1680, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第五箱：{}",box5);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box5, fourPalletCode);
                                                    break;
                                                case 6:
                                                    String box6 = outboundLineBody.readHoldingRegistersString10(1690, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第六箱：{}",box6);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box6, fourPalletCode);
                                                    break;
                                                case 7:
                                                    String box7 = outboundLineBody.readHoldingRegistersString10(1700, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第七箱：{}",box7);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box7, fourPalletCode);
                                                    break;
                                                case 8:
                                                    String box8 = outboundLineBody.readHoldingRegistersString10(1710, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第八箱：{}",box8);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box8, fourPalletCode);
                                                    break;
                                                case 9:
                                                    String box9 = outboundLineBody.readHoldingRegistersString10(1720, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第九箱：{}",box9);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box9, fourPalletCode);
                                                    break;
                                                case 10:
                                                    String box10 = outboundLineBody.readHoldingRegistersString10(1730, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第十箱：{}",box10);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box10, fourPalletCode);
                                                    break;
                                                case 11:
                                                    String box11 = outboundLineBody.readHoldingRegistersString10(1740, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第十一箱：{}",box11);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box11, fourPalletCode);
                                                    break;
                                                case 12:
                                                    String box12 = outboundLineBody.readHoldingRegistersString10(1750, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第十二箱：{}",box12);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box12, fourPalletCode);
                                                    break;
                                                case 13:
                                                    String box13 = outboundLineBody.readHoldingRegistersString(1760, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第十三箱：{}",box13);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box13, fourPalletCode);
                                                    break;
                                                case 14:
                                                    String box14 = outboundLineBody.readHoldingRegistersString10(1770, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第十四箱：{}",box14);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box14, fourPalletCode);
                                                    break;
                                                case 15:
                                                    String box15 = outboundLineBody.readHoldingRegistersString10(1780, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第十五箱：{}",box15);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box15, fourPalletCode);
                                                    break;
                                                case 16:
                                                    String box16 = outboundLineBody.readHoldingRegistersString10(1790, unitId);
                                                    lineBodyLog.info("工单总数等于PLC数量,第四段向下流，第十六箱：{}",box16);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box16, fourPalletCode);
                                                    break;
                                            }
                                        }
                                        boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    } else if (Objects.equals(remainingQuantity, boxSize)) {
                                        lineBodyLog.info("通知plc往前流,剩余数等于PLC数量：{},PLC数量：{}", remainingQuantity, boxSize);
                                        WcsThreeFloorOutbound wcsThreeFloorOutbound1 = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
                                        int id = wcsThreeFloorOutbound1.getId();
                                        Integer completeNumber = wcsThreeFloorOutbound1.getCompleteNumber();
                                        completeNumber = completeNumber + boxSize;
                                        lineBodyLog.info("3楼更新更新出库数量：{}", completeNumber);
                                        //更新出库单，更新完成数
                                        boolean b1 = wcsThreeFloorOutboundService.updateComplete(id, completeNumber);
                                        if (Objects.equals(total, completeNumber)) {
                                            //修改当前出库单完成
                                            wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
                                        }
                                        //将栈板移到历史表
                                        WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
                                        BeanUtils.copyProperties(wcsLineBodyPalletFour, wcsLineBodyPalletHistory);
                                        wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
                                        //删除栈板信息
                                        wcsLineBodyPalletService.deleteId(fourPalletCode);
                                        Thread.sleep(1);
                                        for (Integer i = 1; i <= boxSize; i++) {
                                            switch (i) {
                                                case 1:
                                                    String box1 = outboundLineBody.readHoldingRegistersString10(1640, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第一箱：{}",box1);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box1, fourPalletCode);
                                                    break;
                                                case 2:
                                                    String box2 = outboundLineBody.readHoldingRegistersString10(1650, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第二箱：{}",box2);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box2, fourPalletCode);
                                                    break;
                                                case 3:
                                                    String box3 = outboundLineBody.readHoldingRegistersString10(1660, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第三箱：{}",box3);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box3, fourPalletCode);
                                                    break;
                                                case 4:
                                                    String box4 = outboundLineBody.readHoldingRegistersString10(1670, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第四箱：{}",box4);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box4, fourPalletCode);
                                                    break;
                                                case 5:
                                                    String box5 = outboundLineBody.readHoldingRegistersString10(1680, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第五箱：{}",box5);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box5, fourPalletCode);
                                                    break;
                                                case 6:
                                                    String box6 = outboundLineBody.readHoldingRegistersString10(1690, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第六箱：{}",box6);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box6, fourPalletCode);
                                                    break;
                                                case 7:
                                                    String box7 = outboundLineBody.readHoldingRegistersString10(1700, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第七箱：{}",box7);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box7, fourPalletCode);
                                                    break;
                                                case 8:
                                                    String box8 = outboundLineBody.readHoldingRegistersString10(1710, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第八箱：{}",box8);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box8, fourPalletCode);
                                                    break;
                                                case 9:
                                                    String box9 = outboundLineBody.readHoldingRegistersString10(1720, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第九箱：{}",box9);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box9, fourPalletCode);
                                                    break;
                                                case 10:
                                                    String box10 = outboundLineBody.readHoldingRegistersString10(1730, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第十箱：{}",box10);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box10, fourPalletCode);
                                                    break;
                                                case 11:
                                                    String box11 = outboundLineBody.readHoldingRegistersString10(1740, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第十一箱：{}",box11);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box11, fourPalletCode);
                                                    break;
                                                case 12:
                                                    String box12 = outboundLineBody.readHoldingRegistersString10(1750, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第十二箱：{}",box12);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box12, fourPalletCode);
                                                    break;
                                                case 13:
                                                    String box13 = outboundLineBody.readHoldingRegistersString(1760, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第十三箱：{}",box13);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box13, fourPalletCode);
                                                    break;
                                                case 14:
                                                    String box14 = outboundLineBody.readHoldingRegistersString10(1770, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第十四箱：{}",box14);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box14, fourPalletCode);
                                                    break;
                                                case 15:
                                                    String box15 = outboundLineBody.readHoldingRegistersString10(1780, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第十五箱：{}",box15);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box15, fourPalletCode);
                                                    break;
                                                case 16:
                                                    String box16 = outboundLineBody.readHoldingRegistersString10(1790, unitId);
                                                    lineBodyLog.info("第四段剩余数量等于PLC数量,第十六箱：{}",box16);
                                                    wcsThreeFloorOutboundDetailsService.updateStatusPalletCode(box16, fourPalletCode);
                                                    break;
                                            }
                                        }
                                        boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    } else {
                                        //通知PLC不允许往下流
                                        lineBodyLog.info("4号线体通知plc继续码垛");
                                        boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    }
                                }
                            }
                        }


                        //查询被机械臂标记的料箱
//                    WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = wcsThreeFloorOutboundDetailsService.selectMechanicalArmLabel(TaskType.ROBOTIC_ARM_MARK.getStatus());
//                    if (wcsThreeFloorOutboundDetails != null) {
//                        //将料箱记录到栈板上
//                        String containerCode = wcsThreeFloorOutboundDetails.getContainerCode();
//                        String workOrder = wcsThreeFloorOutboundDetails.getWorkOrder();
//                        boolean b = wcsThreeFloorOutboundDetailsService.updateMechanicalArmLabel(containerCode, TaskType.ROBOTIC_ARM_DEFAULT.getStatus());
//                        WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectWorkOrder(workOrder);
//                        lineBodyLog.info("根据工单号找到栈板：{}", wcsLineBodyPallet);
//                        if (wcsLineBodyPallet != null) {
//                            Integer id = wcsThreeFloorOutboundDetails.getId();
//                            Integer palletId = wcsLineBodyPallet.getPalletId();
//                            Integer lineBodyCode = wcsLineBodyPallet.getLineBodyCode();
//                            lineBodyLog.info("通知plc将料箱放入链条线：{}", lineBodyCode);
//                            //栈板表+1
//                            Integer palletQuantity;
//                            palletQuantity = wcsLineBodyPallet.getPalletQuantity();
//                            palletQuantity++;
//                            //更新栈板数量
//                            lineBodyLog.info("更新栈板号：{},数量：{}", palletId, palletQuantity);
//                            boolean b1 = wcsLineBodyPalletService.updatePalletQuantity(palletId, palletQuantity);
//                            if (!b1) {
//                                lineBodyLog.error("更新栈板数量失败");
//                                throw new RuntimeException("更新栈板数量失败");
//                            }
//                            //记录当前容器所在栈板
//                            boolean b2 = wcsThreeFloorOutboundDetailsService.updatePallerCode(id, palletId);
//                            if (!b2) {
//                                lineBodyLog.error("3楼出库单明细记录所在栈板失败");
//                                throw new RuntimeException("3楼出库单明细记录所在栈板失败");
//                            }
//                            //工单号完成数+1
//                            WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                            if (wcsThreeFloorOutbound != null) {
//                                Integer completeNumber;
//                                completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
//                                completeNumber++;
//                                boolean b4 = wcsThreeFloorOutboundService.updateComplete(wcsThreeFloorOutbound.getId(), completeNumber);
//                                if (!b4) {
//                                    lineBodyLog.error("3楼出库单记录栈板数+1失败");
//                                    throw new RuntimeException("3楼出库单记录栈板数+1失败");
//                                }
//                            }
//
//                        }
//                    }
//
//
//                    //第四段是否允许往下流
//                    Integer fourPalletCode = outboundLineBody.readHoldingRegisters(fourChainPalletCode, unitId);
//                    lineBodyLog.info("第四段往下流栈板码：{}", fourPalletCode);
//                    WcsLineBodyPallet wcsLineBodyPallet = wcsLineBodyPalletService.selectPalletId(fourPalletCode);
//                    if (wcsLineBodyPallet != null) {
//                        Integer twoPlcCount = outboundLineBody.readHoldingRegisters(twoPalletSize, unitId);
//                        Integer palletQuantity = wcsLineBodyPallet.getPalletQuantity();
//                        String workOrder = wcsLineBodyPallet.getWorkOrder();
//                        Integer palletId = wcsLineBodyPallet.getPalletId();
//                        Integer palletSize = 16;
//                        WcsConfiguration wcsConfiguration = wcsConfigurationService.selectKey(TaskType.THREE_FLOOR_OUTBOUND_PALLET_QUANTITY.getMessage());
//                        if (wcsConfiguration != null) {
//                            palletSize = Integer.valueOf(wcsConfiguration.getConfigValue());
//                        }
//                        if (Objects.equals(palletQuantity, palletSize) && Objects.equals(palletQuantity, twoPlcCount)) {
//                            //通知plc往前流
//                            boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                            if (!b) {
//                                lineBodyLog.error("通知plc第一段链条线往下流失败");
//                                throw new RuntimeException("通知plc第一段链条线往下流失败");
//                            }
//                            //将栈板移到历史表
//                            WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
//                            BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
//                            boolean b1 = wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
//                            //删除栈板信息
//                            boolean b2 = wcsLineBodyPalletService.deleteId(palletId);
//                            //查询出库单都完成了修改状态为1
//                            WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                            if (wcsThreeFloorOutbound != null) {
//                                int id = wcsThreeFloorOutbound.getId();
//                                Integer total = wcsThreeFloorOutbound.getTotal();
//                                Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
//                                if (Objects.equals(total, completeNumber)) {
//                                    //修改出库单，机械臂状态
//                                    wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
//                                }
//                            }
//                        } else {
//                            //未满栈板数量，判断剩余数量+栈板数量能不能凑一版，能不出，不能将所有料箱到栈板上才出
//                            WcsThreeFloorOutbound wcsThreeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                            if (wcsThreeFloorOutbound != null) {
//                                Integer total = wcsThreeFloorOutbound.getTotal();
//                                Integer completeNumber = wcsThreeFloorOutbound.getCompleteNumber();
//                                Integer remainingQuantity;
//                                remainingQuantity = total - completeNumber;
//                                if (remainingQuantity < palletSize && remainingQuantity != 0) {
//                                    //通知plc不往下流
//                                    boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                                } else if (remainingQuantity == 0) {
//                                    //通知plc往前流
//                                    boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                                    if (!b) {
//                                        lineBodyLog.error("通知plc第一段链条线往下流失败");
//                                        throw new RuntimeException("通知plc第一段链条线往下流失败");
//                                    }
//                                    //将栈板移到历史表
//                                    WcsLineBodyPalletHistory wcsLineBodyPalletHistory = new WcsLineBodyPalletHistory();
//                                    BeanUtils.copyProperties(wcsLineBodyPallet, wcsLineBodyPalletHistory);
//                                    boolean b1 = wcsLineBodyPalletHistoryService.insert(wcsLineBodyPalletHistory);
//                                    //删除栈板信息
//                                    boolean b2 = wcsLineBodyPalletService.deleteId(palletId);
//
//                                    WcsThreeFloorOutbound threeFloorOutbound = wcsThreeFloorOutboundService.selectWorkOrder(workOrder);
//                                    if (threeFloorOutbound != null) {
//                                        int id = threeFloorOutbound.getId();
//                                        Integer totals = threeFloorOutbound.getTotal();
//                                        Integer completeNumbers = threeFloorOutbound.getCompleteNumber();
//                                        if (Objects.equals(totals, completeNumbers)) {
//                                            //修改出库单，机械臂状态
//                                            wcsThreeFloorOutboundService.updateMechanicalArmComplete(id, 1);
//                                        }
//                                    }
//                                }else {
//                                    boolean b = outboundLineBody.writeWithRetry(twoChainBelowResult, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
//                                }
//                            }
//                        }
//                    }
                    }

                    //栈板流到第三段或第七段，需要通知agv将栈板拉走
                    Integer oneAgvRequestTransport = wcsLineBodyOutbound.getOneAgvRequestTransport();
                    String oneStorageCode = wcsLineBodyOutbound.getOneStorageCode();
                    Integer twoAgvRequestTransport = wcsLineBodyOutbound.getTwoAgvRequestTransport();
                    String twoStorageCode = wcsLineBodyOutbound.getTwoStorageCode();
                    Integer oneAgvTransportPlc = wcsLineBodyOutbound.getOneAgvTransportPlc();
                    Integer twoAgvTransportPlc = wcsLineBodyOutbound.getTwoAgvTransportPlc();
                    Integer oneChainTransportResult = wcsLineBodyOutbound.getOneChainTransportResult();
                    Integer twoChainTransportResult = wcsLineBodyOutbound.getTwoChainTransportResult();
                    Integer oneAgvTransportStatus = outboundLineBody.readHoldingRegisters(oneAgvRequestTransport, unitId);
                    Integer twoAgvTransportStatus = outboundLineBody.readHoldingRegisters(twoAgvRequestTransport, unitId);
                    Integer threeChainPalletCode = wcsLineBodyOutbound.getThreeChainPalletCode();
                    Integer sevenChainPalletCode = wcsLineBodyOutbound.getSevenChainPalletCode();
                    //第一段plc请求取货
                    if (oneAgvTransportStatus == 1) {
                        Integer threePalletCode = outboundLineBody.readHoldingRegisters(threeChainPalletCode, unitId);
                        lineBodyLog.info("查询PLC第三段出库地址位：{},参数：{}", threeChainPalletCode, threePalletCode);
                        if (oneAgvTransportPlc == 0) {
                            //更具栈板号找到配送到产线找空闲呢储位
                            List<WcsThreeFloorOutboundDetails> wcsThreeFloorOutboundDetailsList = wcsThreeFloorOutboundDetailsService.selectPalletCode(String.valueOf(threePalletCode));
                            if (!wcsThreeFloorOutboundDetailsList.isEmpty()) {
                                WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = wcsThreeFloorOutboundDetailsList.get(0);
                                String floor = wcsThreeFloorOutboundDetails.getFloor();
                                String region = wcsThreeFloorOutboundDetails.getRegion();
                                //发送agv任务 创建配送任务，将终点储位预定，发送plc任务中
                                List<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseList = wcsLineEdgeWarehouseService.selectRegionStatusIsNull(region, TaskStatus.LINE_EDGE_WAREHOUSE_IDLE.getStatus());
                                lineBodyLog.info("查询配送储位数据：{}", wcsLineEdgeWarehouseList);
                                if (!wcsLineEdgeWarehouseList.isEmpty()) {
                                    WcsLineEdgeWarehouse wcsLineEdgeWarehouse = wcsLineEdgeWarehouseList.get(0);
                                    String destination = wcsLineEdgeWarehouse.getStorageCode();
                                    lineBodyLog.info("3F出库终点储位：{}", destination);
                                    //储位绑定栈板
                                    String taskId = String.valueOf(idWorker.nextId());
                                    CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
                                    carrierUnbindDto.setSlotCategory(TaskType.SITE.getMessage());
                                    carrierUnbindDto.setSlotCode(oneStorageCode);
                                    carrierUnbindDto.setCarrierCategory("SITE");
                                    carrierUnbindDto.setCarrierType("1");
                                    carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
                                    //                            agvSendService.carrierUnbind(carrierUnbindDto);
                                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                                    //                            agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                                    agvTaskSubmitDto.setTaskType("QC-A1");
                                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                                    TargetRoute targetRoute = new TargetRoute();
                                    TargetRoute targetRoutes = new TargetRoute();
                                    //                        //添加起点
                                    targetRoute.setSeq(0);
                                    targetRoute.setType(TaskType.SITE.getMessage());
                                    targetRoute.setCode(oneStorageCode);
                                    //                        //添加终点
                                    targetRoutes.setSeq(1);
                                    targetRoutes.setType(TaskType.SITE.getMessage());
                                    targetRoutes.setCode(destination);
                                    targetRouteList.add(targetRoute);
                                    targetRouteList.add(targetRoutes);
                                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                                    agvTaskSubmitDto.setRobotTaskCode(taskId);
                                    agvTaskSubmitDto.setInitPriority(PriorityEnum.FOUR.getWeight());
                                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                                    if (taskCode != null) {
                                        //创建配送任务
                                        //创建配送任务
                                        WcsDeliveryMission wcsDeliveryMission = new WcsDeliveryMission();
                                        wcsDeliveryMission.setDeliveryCode(taskId);
                                        wcsDeliveryMission.setMissionStatus(TaskStatus.task_send.getStatus());
                                        wcsDeliveryMission.setSource(oneStorageCode);
                                        wcsDeliveryMission.setDestination(destination);
                                        wcsDeliveryMission.setCreateTime(LocalDateTime.now());
                                        wcsDeliveryMission.setUpdateTime(LocalDateTime.now());
                                        wcsDeliveryMission.setContainerType(TaskType.THREE_FLOOR_OUTBOUND_PLC.getStatus());
                                        //                                wcsDeliveryMission.setReportWms(0);
                                        wcsDeliveryMission.setReportWms(0);
                                        wcsDeliveryMission.setFloor("3");
                                        wcsDeliveryMission.setContainerCode(String.valueOf(threePalletCode));
                                        boolean b1 = wcsDeliveryMissionService.insertDelivery(wcsDeliveryMission);
                                        if (!b1) {
                                            lineBodyLog.error("点对点叫车，创建配送任务失败，数据库异常");
                                            throw new RuntimeException("点对点叫车，创建配送任务失败，数据库异常");
                                        }
                                        //将终点储位预定，
                                        boolean b = wcsLineEdgeWarehouseService.updateStatusIsAgv(destination, null, TaskStatus.LINE_EDGE_WAREHOUSE_RESERVED.getStatus(), 1);
                                        // 发送plc任务中
                                        outboundLineBody.writeWithRetry(oneChainTransportResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    }
                                } else {
                                    //线边储位已满，按顺序找有的储位下发,按照ID排序
                                    List<WcsLineEdgeWarehouse> wcsLineEdgeWarehouses = wcsLineEdgeWarehouseService.selectRegionAndGeStatus(region, TaskStatus.LINE_EDGE_WAREHOUSE_OCCUPIED.getStatus());
                                    lineBodyLog.info("线边仓已满，找大于3的栈板数据：{}", wcsLineEdgeWarehouses);
                                    if (!wcsLineEdgeWarehouses.isEmpty()) {
                                        WcsLineEdgeWarehouse wcsLineEdgeWarehouse = wcsLineEdgeWarehouses.get(0);
                                        String destination = wcsLineEdgeWarehouse.getStorageCode();
                                        lineBodyLog.info("3F出库终点储位：{}", destination);
                                        String taskId = String.valueOf(idWorker.nextId());
                                        AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                                        //                            agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                                        agvTaskSubmitDto.setTaskType("QC-A1");
                                        ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                                        TargetRoute targetRoute = new TargetRoute();
                                        TargetRoute targetRoutes = new TargetRoute();
                                        //                        //添加起点
                                        targetRoute.setSeq(0);
                                        targetRoute.setType(TaskType.SITE.getMessage());
                                        targetRoute.setCode(oneStorageCode);
                                        //                        //添加终点
                                        targetRoutes.setSeq(1);
                                        targetRoutes.setType(TaskType.SITE.getMessage());
                                        targetRoutes.setCode(destination);
                                        targetRouteList.add(targetRoute);
                                        targetRouteList.add(targetRoutes);
                                        agvTaskSubmitDto.setTargetRoute(targetRouteList);
                                        agvTaskSubmitDto.setRobotTaskCode(taskId);
                                        agvTaskSubmitDto.setInitPriority(PriorityEnum.FOUR.getWeight());
                                        String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                                        if (taskCode != null) {
                                            //创建配送任务
                                            WcsDeliveryMission wcsDeliveryMission = new WcsDeliveryMission();
                                            wcsDeliveryMission.setDeliveryCode(taskId);
                                            wcsDeliveryMission.setMissionStatus(TaskStatus.task_send.getStatus());
                                            wcsDeliveryMission.setSource(oneStorageCode);
                                            wcsDeliveryMission.setDestination(destination);
                                            wcsDeliveryMission.setCreateTime(LocalDateTime.now());
                                            wcsDeliveryMission.setUpdateTime(LocalDateTime.now());
                                            wcsDeliveryMission.setContainerType(TaskType.THREE_FLOOR_OUTBOUND_PLC.getStatus());
                                            //                                wcsDeliveryMission.setReportWms(0);
                                            wcsDeliveryMission.setReportWms(0);
                                            wcsDeliveryMission.setFloor("3");
                                            wcsDeliveryMission.setContainerCode(String.valueOf(threePalletCode));
                                            boolean b1 = wcsDeliveryMissionService.insertDelivery(wcsDeliveryMission);
                                            if (!b1) {
                                                lineBodyLog.error("点对点叫车，创建配送任务失败，数据库异常");
                                                throw new RuntimeException("点对点叫车，创建配送任务失败，数据库异常");
                                            }
                                            //将终点储位预定，
                                            boolean b = wcsLineEdgeWarehouseService.updateStatusIsAgv(destination, null, TaskStatus.LINE_EDGE_WAREHOUSE_RESERVED.getStatus(), 1);
                                            // 发送plc任务中
                                            outboundLineBody.writeWithRetry(oneChainTransportResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        //链条线没货标记归0
                        if (oneAgvTransportPlc == 1) {
                            boolean b = wcsLineBodyOutboundService.updateOneAgvTransportPlc(wcsLineBodyOutboundListId, 0);
                        }
                    }

                    //第二段plc请求取货
                    lineBodyLog.info("第二段请求搬运：{}", twoAgvTransportStatus);
                    if (twoAgvTransportStatus == 1) {
                        Integer sevenPalletCode = outboundLineBody.readHoldingRegisters(sevenChainPalletCode, unitId);
                        lineBodyLog.info("第二段读码信息：{}", sevenPalletCode);
                        if (twoAgvTransportPlc == 0) {
                            List<WcsThreeFloorOutboundDetails> wcsThreeFloorOutboundDetailsList = wcsThreeFloorOutboundDetailsService.selectPalletCode(String.valueOf(sevenPalletCode));
                            if (!wcsThreeFloorOutboundDetailsList.isEmpty()) {
                                WcsThreeFloorOutboundDetails wcsThreeFloorOutboundDetails = wcsThreeFloorOutboundDetailsList.get(0);
                                String floor = wcsThreeFloorOutboundDetails.getFloor();
                                String region = wcsThreeFloorOutboundDetails.getRegion();
                                //发送agv任务 创建配送任务，将终点储位预定，发送plc任务中
                                List<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseList = wcsLineEdgeWarehouseService.selectRegionStatusIsNull(region, TaskStatus.LINE_EDGE_WAREHOUSE_IDLE.getStatus());
                                if (!wcsLineEdgeWarehouseList.isEmpty()) {
                                    WcsLineEdgeWarehouse wcsLineEdgeWarehouse = wcsLineEdgeWarehouseList.get(0);
                                    String destination = wcsLineEdgeWarehouse.getStorageCode();
                                    //储位绑定栈板
                                    String taskId = String.valueOf(idWorker.nextId());
                                    CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
                                    carrierUnbindDto.setSlotCategory(TaskType.SITE.getMessage());
                                    carrierUnbindDto.setSlotCode(twoStorageCode);
                                    carrierUnbindDto.setCarrierCategory("SITE");
                                    carrierUnbindDto.setCarrierType("1");
                                    carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
                                    //                            agvSendService.carrierUnbind(carrierUnbindDto);
                                    AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                                    //                            agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                                    agvTaskSubmitDto.setTaskType("QC-A1");
                                    ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                                    TargetRoute targetRoute = new TargetRoute();
                                    TargetRoute targetRoutes = new TargetRoute();
                                    //                        //添加起点
                                    targetRoute.setSeq(0);
                                    targetRoute.setType(TaskType.SITE.getMessage());
                                    targetRoute.setCode(twoStorageCode);
                                    //                        //添加终点
                                    targetRoutes.setSeq(1);
                                    targetRoutes.setType(TaskType.SITE.getMessage());
                                    targetRoutes.setCode(destination);
                                    targetRouteList.add(targetRoute);
                                    targetRouteList.add(targetRoutes);
                                    agvTaskSubmitDto.setTargetRoute(targetRouteList);
                                    agvTaskSubmitDto.setRobotTaskCode(taskId);
                                    agvTaskSubmitDto.setInitPriority(PriorityEnum.FOUR.getWeight());
                                    String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                                    if (taskCode != null) {
                                        //创建配送任务
                                        //创建配送任务
                                        WcsDeliveryMission wcsDeliveryMission = new WcsDeliveryMission();
                                        wcsDeliveryMission.setDeliveryCode(taskId);
                                        wcsDeliveryMission.setMissionStatus(TaskStatus.task_send.getStatus());
                                        wcsDeliveryMission.setSource(twoStorageCode);
                                        wcsDeliveryMission.setDestination(destination);
                                        wcsDeliveryMission.setCreateTime(LocalDateTime.now());
                                        wcsDeliveryMission.setUpdateTime(LocalDateTime.now());
                                        wcsDeliveryMission.setContainerType(TaskType.THREE_FLOOR_OUTBOUND_PLC.getStatus());
                                        wcsDeliveryMission.setReportWms(0);
                                        wcsDeliveryMission.setFloor("3");
                                        wcsDeliveryMission.setContainerCode(String.valueOf(sevenPalletCode));
                                        boolean b1 = wcsDeliveryMissionService.insertDelivery(wcsDeliveryMission);
                                        if (!b1) {
                                            lineBodyLog.error("点对点叫车，创建配送任务失败，数据库异常");
                                            throw new RuntimeException("点对点叫车，创建配送任务失败，数据库异常");
                                        }
                                        //将终点储位预定，
                                        boolean b = wcsLineEdgeWarehouseService.updateStatusIsAgv(destination, null, TaskStatus.LINE_EDGE_WAREHOUSE_RESERVED.getStatus(), 1);
                                        // 发送plc任务中
                                        outboundLineBody.writeWithRetry(twoChainTransportResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                    }
                                } else {
                                    //线边储位已满，按顺序找有的储位下发,按照ID排序
                                    List<WcsLineEdgeWarehouse> wcsLineEdgeWarehouses = wcsLineEdgeWarehouseService.selectRegionAndGeStatus(region, TaskStatus.LINE_EDGE_WAREHOUSE_OCCUPIED.getStatus());
                                    lineBodyLog.info("线边仓已满，找大于3的栈板数据：{}", wcsLineEdgeWarehouses);
                                    if (!wcsLineEdgeWarehouses.isEmpty()) {
                                        WcsLineEdgeWarehouse wcsLineEdgeWarehouse = wcsLineEdgeWarehouses.get(0);
                                        String destination = wcsLineEdgeWarehouse.getStorageCode();
                                        //储位绑定栈板
                                        String taskId = String.valueOf(idWorker.nextId());
                                        AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                                        //                            agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                                        agvTaskSubmitDto.setTaskType("QC-A1");
                                        ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                                        TargetRoute targetRoute = new TargetRoute();
                                        TargetRoute targetRoutes = new TargetRoute();
                                        //                        //添加起点
                                        targetRoute.setSeq(0);
                                        targetRoute.setType(TaskType.SITE.getMessage());
                                        targetRoute.setCode(twoStorageCode);
                                        //                        //添加终点
                                        targetRoutes.setSeq(1);
                                        targetRoutes.setType(TaskType.SITE.getMessage());
                                        targetRoutes.setCode(destination);
                                        targetRouteList.add(targetRoute);
                                        targetRouteList.add(targetRoutes);
                                        agvTaskSubmitDto.setTargetRoute(targetRouteList);
                                        agvTaskSubmitDto.setRobotTaskCode(taskId);
                                        agvTaskSubmitDto.setInitPriority(PriorityEnum.FOUR.getWeight());
                                        String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                                        if (taskCode != null) {
                                            //创建配送任务
                                            //创建配送任务
                                            WcsDeliveryMission wcsDeliveryMission = new WcsDeliveryMission();
                                            wcsDeliveryMission.setDeliveryCode(taskId);
                                            wcsDeliveryMission.setMissionStatus(TaskStatus.task_send.getStatus());
                                            wcsDeliveryMission.setSource(twoStorageCode);
                                            wcsDeliveryMission.setDestination(destination);
                                            wcsDeliveryMission.setCreateTime(LocalDateTime.now());
                                            wcsDeliveryMission.setUpdateTime(LocalDateTime.now());
                                            wcsDeliveryMission.setContainerType(TaskType.THREE_FLOOR_OUTBOUND_PLC.getStatus());
                                            wcsDeliveryMission.setReportWms(0);
                                            wcsDeliveryMission.setFloor("3");
                                            wcsDeliveryMission.setContainerCode(String.valueOf(sevenPalletCode));
                                            boolean b1 = wcsDeliveryMissionService.insertDelivery(wcsDeliveryMission);
                                            if (!b1) {
                                                lineBodyLog.error("点对点叫车，创建配送任务失败，数据库异常");
                                                throw new RuntimeException("点对点叫车，创建配送任务失败，数据库异常");
                                            }
                                            //将终点储位预定，
                                            boolean b = wcsLineEdgeWarehouseService.updateStatusIsAgv(destination, null, TaskStatus.LINE_EDGE_WAREHOUSE_RESERVED.getStatus(), 1);
                                            // 发送plc任务中
                                            outboundLineBody.writeWithRetry(twoChainTransportResult, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        //链条线没货标记归0
                        if (twoAgvTransportPlc == 1) {
                            boolean b = wcsLineBodyOutboundService.updateTwoAgvTransportPlc(wcsLineBodyOutboundListId, 0);
                        }
                    }

                    //读码写入过长，归0
                    LocalDateTime readTimi = wcsLineBodyOutbound.getReadTimi();
                    LocalDateTime now = LocalDateTime.now();
                    if (readTimi != null) {
                        long oneWrite = Duration.between(readTimi, now).getSeconds();
                        if (oneWrite > 60 && readCodeWritePlc == 1) {
                            //无货标记set为0
                            boolean b3 = wcsLineBodyOutboundService.updateReadCodeWritePlc(wcsLineBodyOutboundListId, 0);
                        }
                    }
                }
            }
        } catch (RuntimeException e) {
            lineBodyLog.error("异常信息：", e);
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            lineBodyLog.error("异常信息：", e);
            throw new RuntimeException(e);
        }
    }

}
